﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using e77.Ud2;
using Microsoft.CSharp;

namespace Szakdolgozat
{
    class Measure
    {
        Dictionary<EColors, double> Results = new Dictionary<EColors, double>();
        double Int;

        // a measure parancsot dolgozza csak fel egyelőre, a visszakapott byte-okat dolgozza fel
        public string measurecomp(byte[] data, Strip.EPadNames padname, out double cutoff, out Dictionary<EColors, double> leds)
        {
            byte[] green;
            byte[] cyan;
            byte[] orange;
            byte[] red;

            // ezeknek a feltételeknek teljesülni kell, különben nem is lehet jó a visszakapott adat
            // tehát a fejlécnek, a helyes parancsnak és a helyes hossznak meg kell lenni
            if (data[0] == 0xaa && data[1] == 0x55 && data[2] == 0x03 && data[3] == 10)
            {

                green = new byte[] { data[4], data[5] };
                cyan = new byte[] { data[6], data[7] };
                orange = new byte[] { data[8], data[9] };
                red = new byte[] { data[10], data[11] };

                Results = new Dictionary<EColors, double>();

                if (Results.Count == 0)
                {
                    Results.Add(EColors.green, BitConverter.ToInt16(green, 0));
                    Results.Add(EColors.cyan, BitConverter.ToInt16(cyan, 0));
                    Results.Add(EColors.orange, BitConverter.ToInt16(orange, 0));
                    Results.Add(EColors.red, BitConverter.ToInt16(red, 0));
                }

                else
                {
                    Results[EColors.green] = BitConverter.ToInt16(green, 0);
                    Results[EColors.cyan] = BitConverter.ToInt16(cyan, 0);
                    Results[EColors.orange] = BitConverter.ToInt16(orange, 0);
                    Results[EColors.red] = BitConverter.ToInt16(red, 0);
                }

                leds = Results;
                byte[] light = green.Concat(cyan).Concat(orange).Concat(red).ToArray();

                if (BitConverter.ToInt32(light, 0) == 0xFFFF)
                {
                    cutoff = -1;
                    return "overlit channel";
                }
                else if (BitConverter.ToInt32(light, 0) == 0xFFF)
                {
                    cutoff = -1;
                    return "LED error";
                }

                Int = 0;
                foreach (var item in Results.Values)
                {
                    Int += Math.Pow(item, 2);
                }

                Int = Math.Sqrt(Int);

                Dictionary<Strip.EPadNames, double> Values = new Dictionary<Strip.EPadNames, double>()
                {
                    {Strip.EPadNames.Bil, this.Bil},
                    {Strip.EPadNames.Ubg, this.Ubg},
                    {Strip.EPadNames.Ket, this.Ket},
                    {Strip.EPadNames.Asc, this.Asc},
                    {Strip.EPadNames.Glu, this.Glu},
                    {Strip.EPadNames.Pro, this.Pro},
                    {Strip.EPadNames.Ery, this.Ery},
                    {Strip.EPadNames.pH, this.pH},
                    {Strip.EPadNames.Nit, this.Nit},
                    {Strip.EPadNames.Leu, this.Leu},
                    {Strip.EPadNames.SG, this.SG},
                };

                if (padname.ToString() != "Comp")
                    cutoff = Values[padname];
                else
                    cutoff = 0;

                return "result";
            }
            else
            {
                leds = null;
                cutoff = -1;
                return "error";
            }

        }


        // a legtöbb pad-nél normalizált értékekekkel kell számolni, ennek kiszámítása:
        private double Normalize(double value)
        {
            return (10000 / Int) * value;
        }



        /*/
        Dictionary<int, Thresholds> DBil1 = new Dictionary<int, Thresholds>
        {
            {0, new Thresholds("neg", ">", 9965, 21 )},
            {1, new Thresholds("tr", ">", 9856,	35)},
            {2, new Thresholds("1+", ">", 9650,	50)},
            {3, new Thresholds("2+", ">", 9400,	50)},
            {4, new Thresholds("3+", "<=", 9400, 0 )},                    
        };

        Dictionary<int, Thresholds> DBil2 = new Dictionary<int, Thresholds>
        {
            {0, new Thresholds("neg", "<", 4950, -70)},                 
        };
        /*/
        Dictionary<int, int> DBil2 = new Dictionary<int, int> { { 0, 4950 } };
        Dictionary<int, int> DBil1 = new Dictionary<int, int>
        {
            {0, 9965},
            {1, 9856},
            {2, 9650},
            {3, 9650},
            {4, 9400},
            {5, 9400}
        };
        //*/


        #region Bil
        // 1.: először az R-0.6*G képletből vizsgálni a  <4950 értékeket, a neg kategóriát megadni									
        // 2.: ha >4950 az R-0.6*G képlet alapján, akkor vizsgálja a NG+NR képlet alapján az értékeket, majd ebből adja a kategóriát

        protected int Bil
        {
            get
            {
                if (Results[EColors.red] - 0.6 * Results[EColors.green] < DBil2[0])
                    return 0;
                else
                {
                    foreach (KeyValuePair<int, int> threshold in DBil1)
                    {
                        if (threshold.Value < Normalize(Results[EColors.red] + Results[EColors.green]))
                            return threshold.Key;
                    }
                    return 6;
                }
            }
        }


      #endregion


        #region Ubg

        // NB+0,8NR
        private double Ubg
        {
            get
            {
                return Normalize(Results[EColors.cyan] + 0.8 * Results[EColors.red]);
            }
        }

        #endregion


        #region Ket
        // Int-2*(R-O)
        private double Ket
        {
            get
            {
                return Int - 2 * (Results[EColors.red] - Results[EColors.orange]);
            }
        }

        #endregion


        #region Asc
        //NB+0.5NO
        private double Asc
        {
            get
            {
                return Normalize(Results[EColors.cyan] + 0.5 * Results[EColors.orange]);
            }
        }
        #endregion


        #region Glu
        //Glu Int(ROGB) alapján!
        private double Glu
        {
            get
            {
                return Int;
            }
        }
        #endregion


        #region Pro
        //NO-0.3NR
        private double Pro
        {
            get
            {
                return Normalize(Results[EColors.orange] - 0.3 * Results[EColors.red]);
            }
        }
        #endregion


        #region Ery
        //Ha Int(ROGB) < 12056, akkor Int (ROGB), egyébként 5.5435*(R-0.35*G)
        private double Ery
        {
            get
            {
                if (Int < 12056)
                    return Int;
                else
                    return 5.5435 * (Results[EColors.red] - 0.35 * Results[EColors.green]);
            }
        }
        #endregion


        #region pH
        //ha NO-NB<0 , akkor NO-NB, egyébként NO-NG+706
        private double pH
        {
            get
            {
                double temp = Normalize(Results[EColors.orange] - Results[EColors.cyan]);
                if (temp < 0)
                    return temp;
                else
                    return Results[EColors.orange] - Results[EColors.green] + 706;
            }
        }
        #endregion


        #region Nit
        // Nit G alapján megy
        private double Nit
        {
            get
            {
                return Results[EColors.green];
            }
        }
        #endregion


        #region Leu
        //NR-NO
        private double Leu
        {
            get
            {
                return Normalize(Results[EColors.red] - Results[EColors.orange]);
            }
        }
        #endregion


        #region SG
        //NG-0.5*Komp, ahol Komp=ÖB-ÖR, ahol ÖB=-1.0306*NB+9860.67 és ÖR=NR
        private double SG
        {

            get
            {
                double Komp = Normalize(-1.0306 * Results[EColors.cyan]) + 9860.67 - Normalize(Results[EColors.red]);
                return Normalize(Results[EColors.green]) - 0.5 * Komp;
            }
        }

        #endregion

    }
}


